home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / DirectPlay / Tutorials / Tut08_Voice / Voice.cpp < prev    next >
C/C++ Source or Header  |  2001-10-08  |  41KB  |  1,240 lines

  1. //----------------------------------------------------------------------------
  2. // File: Voice.cpp
  3. //
  4. // Desc: This simple program builds upon the last tutorial and adds voice support
  5. //
  6. // Copyright (c) 2000-2001 Microsoft Corp. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #define INITGUID
  9. #define _WIN32_DCOM
  10. #include <stdio.h>
  11. #include <dplay8.h>
  12. #include <dplobby8.h>
  13. #include <dvoice.h>
  14.  
  15.  
  16.  
  17. //-----------------------------------------------------------------------------
  18. // App specific structures 
  19. //-----------------------------------------------------------------------------
  20. struct HOST_NODE
  21. {
  22.     DPN_APPLICATION_DESC*   pAppDesc;
  23.     IDirectPlay8Address*    pHostAddress;
  24.     WCHAR*                  pwszSessionName;
  25.  
  26.     HOST_NODE*              pNext;
  27. };
  28.  
  29.  
  30. //-----------------------------------------------------------------------------
  31. // Global variables
  32. //-----------------------------------------------------------------------------
  33. IDirectPlay8Peer*                   g_pDP               = NULL;
  34. IDirectPlay8LobbiedApplication*     g_pLobbyApp         = NULL;
  35. IDirectPlay8Address*                g_pDeviceAddress    = NULL;
  36. IDirectPlay8Address*                g_pHostAddress      = NULL;
  37. IDirectPlayVoiceClient*             g_pVoiceClient      = NULL;
  38. IDirectPlayVoiceServer*             g_pVoiceServer      = NULL;
  39. DPNHANDLE                           g_hLobbyHandle      = NULL;
  40. BOOL                                g_bLobbyLaunched    = FALSE;
  41. BOOL                                g_bHost;
  42. BOOL                                g_bRegister         = FALSE;
  43. BOOL                                g_bUnRegister       = FALSE;
  44. WCHAR*                              g_wszPath           = NULL;
  45. HOST_NODE*                          g_pHostList         = NULL;
  46. CRITICAL_SECTION                    g_csHostList;
  47. DPNID                               g_dpnidLocalPlayer  = 0;
  48.  
  49.  
  50. // This GUID allows DirectPlay to find other instances of the same game on
  51. // the network.  So it must be unique for every game, and the same for 
  52. // every instance of that game.  // {91F570F2-CE88-428a-8025-25A1E4918B44}
  53. GUID g_guidApp = { 0x91f570f2, 0xce88, 0x428a, { 0x80, 0x25, 0x25, 0xa1, 0xe4, 0x91, 0x8b, 0x44 } };
  54.  
  55.  
  56. //-----------------------------------------------------------------------------
  57. // Function-prototypes
  58. //-----------------------------------------------------------------------------
  59. HRESULT WINAPI DirectPlayMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer);
  60. HRESULT WINAPI LobbyAppMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer);
  61. HRESULT WINAPI DirectVoiceServerMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer);
  62. HRESULT WINAPI DirectVoiceClientMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer);
  63. BOOL    IsServiceProviderValid(const GUID* pGuidSP);
  64. HRESULT InitDirectPlay();
  65. HRESULT InitDirectPlayVoice();
  66. HRESULT CreateDeviceAddress();
  67. HRESULT CreateHostAddress(WCHAR* pwszHost);
  68. HRESULT HostSession();
  69. HRESULT EnumDirectPlayHosts();
  70. HRESULT ConnectToSession();
  71. HRESULT SendDirectPlayMessage();
  72. HRESULT Register();
  73. HRESULT UnRegister();
  74. HRESULT LobbyLaunch();
  75. HRESULT TestDirectVoice();
  76. HWND    GetConsoleHwnd();
  77. void    CleanupDirectPlay();
  78.  
  79.  
  80. //-----------------------------------------------------------------------------
  81. // Miscellaneous helper functions
  82. //-----------------------------------------------------------------------------
  83. #define SAFE_DELETE(p)          {if(p) {delete (p);     (p)=NULL;}}
  84. #define SAFE_DELETE_ARRAY(p)    {if(p) {delete[] (p);   (p)=NULL;}}
  85. #define SAFE_RELEASE(p)         {if(p) {(p)->Release(); (p)=NULL;}}
  86.  
  87. #define USER_HOST       1
  88. #define USER_CONNECT    2
  89. #define USER_EXIT       1
  90. #define USER_SEND       2
  91.  
  92. #define CMDLINE_REGISTER    "register"
  93. #define CMDLINE_UNREGISTER  "unregister"
  94.  
  95.  
  96.  
  97.  
  98. //-----------------------------------------------------------------------------
  99. // Name: main()
  100. // Desc: Entry point for the application.  
  101. //-----------------------------------------------------------------------------
  102. int main(int argc, char* argv[], char* envp[])
  103. {
  104.     HRESULT                     hr;
  105.     int                         iUserChoice;
  106.     int                         i;
  107.  
  108.     // Init COM so we can use CoCreateInstance
  109.     CoInitializeEx(NULL, COINIT_MULTITHREADED);
  110.     InitializeCriticalSection(&g_csHostList);
  111.  
  112.     // Process the args
  113.     for (i = 1; i < argc, argv[i] != NULL; i++)
  114.     {
  115.         if( !(strcmp(CMDLINE_REGISTER, argv[i] ) ) )
  116.         {
  117.             g_bRegister = TRUE;
  118.             g_wszPath = new WCHAR[strlen(argv[0]) + 1];
  119.  
  120.             if( !g_wszPath)
  121.             {
  122.                 printf("Failed allocating string\n");
  123.                 goto LCleanup;
  124.             }
  125.  
  126.             MultiByteToWideChar(CP_ACP, 0, argv[0], -1, g_wszPath, strlen(argv[0]) + 1);
  127.         }
  128.         else if( !strcmp(CMDLINE_UNREGISTER, argv[i]))
  129.         {
  130.             g_bUnRegister = TRUE;
  131.         }
  132.         else if( !strcmp("-?", argv[i]))
  133.         {
  134.             printf("\nUsage:  register/unregister\n");
  135.             goto LCleanup;
  136.         }
  137.     }
  138.  
  139.     // Init the DirectPlay system
  140.     if( FAILED( hr = InitDirectPlay() ) )
  141.     {
  142.         printf("Failed Initializing DirectPlay:  0x%X\n", hr);
  143.         goto LCleanup;
  144.     }
  145.  
  146.     if( g_bRegister )
  147.     {
  148.         if( FAILED( hr = Register() ) )
  149.         {
  150.             printf("Failed To Register:  0x%X\n", hr);
  151.         }
  152.         goto LCleanup;
  153.     }
  154.     else if( g_bUnRegister )
  155.     {
  156.         if( FAILED( hr = UnRegister() ) )
  157.         {
  158.             printf("Failed To Unregister:  0x%X\n", hr);
  159.         }
  160.         goto LCleanup;
  161.     }
  162.  
  163.     // See if we were lobby launched or not
  164.     if( g_bLobbyLaunched )
  165.     {
  166.         if( FAILED( hr = LobbyLaunch() ) )
  167.         {
  168.             printf("Failed be Lobby Launched:  0x%X\n", hr);
  169.             goto LCleanup;
  170.         }
  171.     }
  172.     else
  173.     {
  174.         // Get the necessary user input on whether they are hosting or connecting
  175.         do
  176.         {
  177.             printf("Please select one.\n1.  Host\n2.  Connect\n");
  178.             scanf("%d", &iUserChoice);
  179.         } while (iUserChoice != USER_HOST && iUserChoice != USER_CONNECT);
  180.  
  181.  
  182.         if( FAILED( hr = CreateDeviceAddress() ) )
  183.         {
  184.             printf("Failed CreatingDeviceAddress:  0x%X\n", hr);
  185.             goto LCleanup;
  186.         }
  187.  
  188.         if( iUserChoice == USER_HOST )
  189.         {
  190.             g_bHost = TRUE;
  191.             if( FAILED( hr = HostSession() ) )
  192.             {
  193.                 printf("Failed Hosting:  0x%X\n", hr);
  194.                 goto LCleanup;
  195.             }
  196.         }
  197.         else
  198.         {
  199.             g_bHost = FALSE;
  200.             if( FAILED( hr = EnumDirectPlayHosts() ) )
  201.             {
  202.                 printf("Failed Enumerating Host:  0x%X\n", hr);
  203.                 goto LCleanup;
  204.             }
  205.  
  206.             if( FAILED( hr = ConnectToSession() ) )
  207.             {
  208.                 printf("Failed Connect to Host:  0x%X\n", hr);
  209.                 goto LCleanup;
  210.             }
  211.             else
  212.             {
  213.                 printf("\nConnection Successful.\n");
  214.             }
  215.         }
  216.     }
  217.  
  218.     // Init DirectVoice
  219.     if( FAILED( hr = InitDirectPlayVoice() ) )
  220.     {
  221.         printf("Failed Initializing DirectVoice:  0x%X\n", hr);
  222.         goto LCleanup;
  223.     }
  224.  
  225.     // Present User with Choices
  226.     do
  227.     {
  228.         printf("Please select one.\n1.  Exit\n2.  Send Data\n");
  229.         scanf("%d", &iUserChoice);
  230.  
  231.         if( iUserChoice == USER_SEND )
  232.         {
  233.             if( FAILED( hr = SendDirectPlayMessage() ) )
  234.             {
  235.                 printf("Failed To Send Data:  0x%X\n", hr);
  236.                 goto LCleanup;
  237.             }
  238.         }
  239.     } while (iUserChoice != USER_EXIT);    
  240.  
  241. LCleanup:
  242.     CleanupDirectPlay();
  243.  
  244.     // ShutDown COM
  245.     CoUninitialize();
  246.  
  247.     return 0;
  248. }
  249.  
  250.  
  251.  
  252.  
  253. //-----------------------------------------------------------------------------
  254. // Name: InitDirectPlay()
  255. // Desc: Initialize DirectPlay
  256. //-----------------------------------------------------------------------------
  257. HRESULT InitDirectPlay()
  258. {
  259.     HRESULT     hr = S_OK;
  260.  
  261.     // Create the IDirectPlay8Peer Object
  262.     if( FAILED( hr = CoCreateInstance(CLSID_DirectPlay8Peer, NULL, 
  263.                                     CLSCTX_INPROC_SERVER,
  264.                                     IID_IDirectPlay8Peer, 
  265.                                     (LPVOID*) &g_pDP ) ) )
  266.     {
  267.         printf("Failed Creating the IDirectPlay8Peer Object:  0x%X\n", hr);
  268.         goto LCleanup;
  269.     }
  270.  
  271.     // Create the IDirectPlay8LobbiedApplication Object
  272.     if( FAILED( hr = CoCreateInstance(CLSID_DirectPlay8LobbiedApplication, NULL, 
  273.                                     CLSCTX_INPROC_SERVER,
  274.                                     IID_IDirectPlay8LobbiedApplication, 
  275.                                     (LPVOID*) &g_pLobbyApp ) ) )
  276.     {
  277.         printf("Failed Creating the IDirectPlay8LobbiedApplication Object:  0x%X\n", hr);
  278.         goto LCleanup;
  279.     }
  280.  
  281.     // Init DirectPlay
  282.     if( FAILED( hr = g_pDP->Initialize(NULL, DirectPlayMessageHandler, 0 ) ) )
  283.     {
  284.         printf("Failed Initializing DirectPlay:  0x%X\n", hr);
  285.         goto LCleanup;
  286.     }
  287.     
  288.     // Init the Lobby interface
  289.     if( FAILED( hr = g_pLobbyApp->Initialize(NULL, LobbyAppMessageHandler, &g_hLobbyHandle, 0 ) ) )
  290.     {
  291.         printf("Failed Initializing Lobby:  0x%X\n", hr);
  292.         goto LCleanup;
  293.     }
  294.     else
  295.         g_bLobbyLaunched = g_hLobbyHandle != NULL;
  296.     
  297.     // Ensure that TCP/IP is a valid Service Provider
  298.     if( FALSE == IsServiceProviderValid(&CLSID_DP8SP_TCPIP ) )
  299.     {
  300.         hr = E_FAIL;
  301.         printf("Failed validating CLSID_DP8SP_TCPIP");
  302.         goto LCleanup;
  303.     }
  304.  
  305. LCleanup:
  306.     return hr;
  307. }
  308.  
  309.  
  310.  
  311.  
  312. //-----------------------------------------------------------------------------
  313. // Name: IsServiceProviderValid()
  314. // Desc: Return TRUE if the service provider is valid
  315. //-----------------------------------------------------------------------------
  316. BOOL IsServiceProviderValid(const GUID* pGuidSP)
  317. {
  318.     HRESULT                     hr;
  319.     DPN_SERVICE_PROVIDER_INFO*  pdnSPInfo = NULL;
  320.     DWORD                       dwItems = 0;
  321.     DWORD                       dwSize = 0;
  322.  
  323.     hr = g_pDP->EnumServiceProviders(&CLSID_DP8SP_TCPIP, NULL, NULL, &dwSize, &dwItems, 0);
  324.  
  325.     if( hr != DPNERR_BUFFERTOOSMALL)
  326.     {
  327.         printf("Failed Enumerating Service Providers:  0x%x\n", hr);
  328.         goto LCleanup;
  329.     }
  330.  
  331.     pdnSPInfo = (DPN_SERVICE_PROVIDER_INFO*) new BYTE[dwSize];
  332.  
  333.     if( FAILED( hr = g_pDP->EnumServiceProviders(&CLSID_DP8SP_TCPIP, NULL, pdnSPInfo, &dwSize, &dwItems, 0 ) ) )
  334.     {
  335.         printf("Failed Enumerating Service Providers:  0x%x\n", hr);
  336.         goto LCleanup;
  337.     }
  338.  
  339.     // There are no items returned so the requested SP is not available
  340.     if( dwItems == 0)
  341.     {
  342.         hr = E_FAIL;
  343.     }
  344.  
  345. LCleanup:
  346.     SAFE_DELETE_ARRAY(pdnSPInfo);
  347.     if( SUCCEEDED(hr) )
  348.         return TRUE;
  349.     else
  350.         return FALSE;
  351. }
  352.  
  353.  
  354.  
  355.  
  356. //-----------------------------------------------------------------------------
  357. // Name: DirectPlayMessageHandler
  358. // Desc: Handler for DirectPlay messages.  
  359. //-----------------------------------------------------------------------------
  360. HRESULT WINAPI DirectPlayMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer)
  361. {
  362.     HRESULT     hr = S_OK;
  363.  
  364.     switch (dwMessageId)
  365.     {
  366.         case DPN_MSGID_ENUM_HOSTS_RESPONSE:
  367.         {
  368.             PDPNMSG_ENUM_HOSTS_RESPONSE     pEnumHostsResponseMsg;
  369.             const DPN_APPLICATION_DESC*     pAppDesc;
  370.             HOST_NODE*                      pHostNode = NULL;
  371.             WCHAR*                          pwszSession = NULL;
  372.  
  373.             pEnumHostsResponseMsg = (PDPNMSG_ENUM_HOSTS_RESPONSE) pMsgBuffer;
  374.             pAppDesc = pEnumHostsResponseMsg->pApplicationDescription;
  375.  
  376.             // Insert each host response if it isn't already present
  377.             EnterCriticalSection(&g_csHostList);
  378.  
  379.             for (pHostNode = g_pHostList; pHostNode; pHostNode = pHostNode->pNext)
  380.             {
  381.                 if( pAppDesc->guidInstance == pHostNode->pAppDesc->guidInstance)
  382.                 {
  383.                     // This host is already in the list
  384.                     pHostNode = NULL;
  385.                     goto Break_ENUM_HOSTS_RESPONSE;
  386.                 }
  387.             }
  388.  
  389.             // This host session is not in the list then so insert it.
  390.             pHostNode = new HOST_NODE;
  391.             if( pHostNode == NULL)
  392.             {
  393.                 goto Break_ENUM_HOSTS_RESPONSE;
  394.             }
  395.  
  396.             ZeroMemory(pHostNode, sizeof(HOST_NODE));
  397.  
  398.             // Copy the Host Address
  399.             if( FAILED( pEnumHostsResponseMsg->pAddressSender->Duplicate(&pHostNode->pHostAddress ) ) )
  400.             {
  401.                 goto Break_ENUM_HOSTS_RESPONSE;
  402.             }
  403.  
  404.             pHostNode->pAppDesc = new DPN_APPLICATION_DESC;
  405.  
  406.             if( pHostNode == NULL)
  407.             {
  408.                 goto Break_ENUM_HOSTS_RESPONSE;
  409.             }
  410.  
  411.             ZeroMemory(pHostNode->pAppDesc, sizeof(DPN_APPLICATION_DESC));
  412.             memcpy(pHostNode->pAppDesc, pAppDesc, sizeof(DPN_APPLICATION_DESC));
  413.  
  414.             // Null out all the pointers we aren't copying
  415.             pHostNode->pAppDesc->pwszSessionName = NULL;
  416.             pHostNode->pAppDesc->pwszPassword = NULL;
  417.             pHostNode->pAppDesc->pvReservedData = NULL;
  418.             pHostNode->pAppDesc->dwReservedDataSize = 0;
  419.             pHostNode->pAppDesc->pvApplicationReservedData = NULL;
  420.             pHostNode->pAppDesc->dwApplicationReservedDataSize = 0;
  421.             
  422.             if( pAppDesc->pwszSessionName)
  423.             {
  424.                 pwszSession = new WCHAR[wcslen(pAppDesc->pwszSessionName) + 1];
  425.                 
  426.                 if( pwszSession)
  427.                 {
  428.                     wcscpy(pwszSession, pAppDesc->pwszSessionName);
  429.                 }
  430.             }
  431.  
  432.             pHostNode->pwszSessionName = pwszSession;
  433.  
  434.             // Insert it onto the front of the list
  435.             pHostNode->pNext = g_pHostList ? g_pHostList->pNext : NULL;
  436.             g_pHostList = pHostNode;
  437.             pHostNode = NULL;
  438.  
  439. Break_ENUM_HOSTS_RESPONSE:
  440.             LeaveCriticalSection(&g_csHostList);
  441.  
  442.             if( pHostNode)
  443.             {
  444.                 SAFE_RELEASE(pHostNode->pHostAddress);
  445.  
  446.                 SAFE_DELETE(pHostNode->pAppDesc);
  447.  
  448.                 delete pHostNode;
  449.             }
  450.  
  451.             break;
  452.         }
  453.     
  454.         case DPN_MSGID_RECEIVE:
  455.         {
  456.             PDPNMSG_RECEIVE     pMsg;
  457.  
  458.             pMsg = (PDPNMSG_RECEIVE) pMsgBuffer;
  459.  
  460.             printf("\nReceived Message:  %S\n", (WCHAR*)pMsg->pReceiveData);
  461.             break;
  462.         }
  463.     
  464.         case DPN_MSGID_HOST_MIGRATE:
  465.         {
  466.             PDPNMSG_HOST_MIGRATE    pHostMigrateMsg;
  467.  
  468.             pHostMigrateMsg = (PDPNMSG_HOST_MIGRATE) pMsgBuffer;
  469.  
  470.             printf("\nHost Migration Has Occured.\n");
  471.  
  472.             // See if we are the new host
  473.             if( pHostMigrateMsg->dpnidNewHost == g_dpnidLocalPlayer)
  474.                 printf("You are the New Host\n");
  475.             else
  476.                 printf("DPNID of New Host is %d\n", pHostMigrateMsg->dpnidNewHost);
  477.  
  478.             break;
  479.         }
  480.     
  481.         case DPN_MSGID_CREATE_PLAYER:
  482.         {
  483.             PDPNMSG_CREATE_PLAYER   pCreatePlayerMsg;
  484.             DWORD                   dwSize = 0;
  485.             DPN_PLAYER_INFO*        pdpPlayerInfo = NULL;
  486.  
  487.             pCreatePlayerMsg = (PDPNMSG_CREATE_PLAYER)pMsgBuffer;
  488.  
  489.             // check to see if we are the player being created
  490.             hr = g_pDP->GetPeerInfo(pCreatePlayerMsg->dpnidPlayer, pdpPlayerInfo, &dwSize, 0);
  491.  
  492.             if( FAILED( hr) && hr != DPNERR_BUFFERTOOSMALL)
  493.             {
  494.                 printf("Failed GetPeerInfo:  0x%X\n", hr);
  495.                 return hr;
  496.             }
  497.  
  498.             pdpPlayerInfo = (DPN_PLAYER_INFO*) new BYTE[dwSize];
  499.             ZeroMemory(pdpPlayerInfo, dwSize);
  500.             pdpPlayerInfo->dwSize = sizeof(DPN_PLAYER_INFO);
  501.  
  502.             if( FAILED( hr = g_pDP->GetPeerInfo(pCreatePlayerMsg->dpnidPlayer, pdpPlayerInfo, &dwSize, 0 ) ) )
  503.             {
  504.                 printf("Failed GetPeerInfo:  0x%X\n", hr);
  505.                 goto Error_DPN_MSGID_CREATE_PLAYER;
  506.             }
  507.  
  508.             if( pdpPlayerInfo->dwPlayerFlags & DPNPLAYER_LOCAL)
  509.                 g_dpnidLocalPlayer = pCreatePlayerMsg->dpnidPlayer;
  510.  
  511. Error_DPN_MSGID_CREATE_PLAYER:
  512.             SAFE_DELETE_ARRAY(pdpPlayerInfo);
  513.             break;
  514.         }
  515.     }
  516.     return hr;
  517. }
  518.  
  519.  
  520.  
  521.  
  522. //-----------------------------------------------------------------------------
  523. // Name: LobbyAppMessageHandler
  524. // Desc: Handler for DirectPlay lobby messages
  525. //-----------------------------------------------------------------------------
  526. HRESULT WINAPI LobbyAppMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer)
  527. {
  528.     HRESULT     hr = S_OK;
  529.  
  530.     switch (dwMessageId)
  531.     {
  532.         case DPL_MSGID_CONNECT:
  533.         {
  534.             PDPL_MESSAGE_CONNECT        pConnectMsg;
  535.             PDPL_CONNECTION_SETTINGS    pSettings;
  536.  
  537.             pConnectMsg = (PDPL_MESSAGE_CONNECT)pMsgBuffer;
  538.             pSettings = pConnectMsg->pdplConnectionSettings;
  539.  
  540.             // Register the lobby with directplay so we get automatic notifications
  541.             hr = g_pDP->RegisterLobby(pConnectMsg->hConnectId, g_pLobbyApp, DPNLOBBY_REGISTER);
  542.             break;
  543.         }
  544.     }
  545.     return hr;
  546. }
  547.  
  548.  
  549.  
  550.  
  551. //-----------------------------------------------------------------------------
  552. // Name: DirectVoiceServerMessageHandler
  553. // Desc: Handler for DirectPlay Voice server messages
  554. //-----------------------------------------------------------------------------
  555. HRESULT WINAPI DirectVoiceServerMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer)
  556. {
  557.     // In this app we are not responding to any server messages
  558.     return S_OK;
  559. }
  560.  
  561.  
  562.  
  563.  
  564. //-----------------------------------------------------------------------------
  565. // Name: DirectVoiceClientMessageHandler
  566. // Desc: Handler for DirectPlay Voice client messages
  567. //-----------------------------------------------------------------------------
  568. HRESULT WINAPI DirectVoiceClientMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer)
  569. {
  570.     HRESULT     hr = S_OK;
  571.  
  572.     switch(dwMessageId)
  573.     {
  574.         case DVMSGID_RECORDSTART:
  575.         {
  576.             printf("Record Start\n");
  577.             break;
  578.         }
  579.  
  580.         case DVMSGID_RECORDSTOP:
  581.         {
  582.             printf("Record Stop\n");
  583.             break;
  584.         }
  585.     }
  586.  
  587.     return hr;
  588. }
  589.  
  590.  
  591.  
  592.  
  593. //-----------------------------------------------------------------------------
  594. // Name: EnumDirectPlayHosts()
  595. // Desc: Enumerates the hosts
  596. //-----------------------------------------------------------------------------
  597. HRESULT EnumDirectPlayHosts()
  598. {
  599.     HRESULT                 hr = S_OK;
  600.     WCHAR                   wszHost[128];
  601.     DPN_APPLICATION_DESC    dpAppDesc;
  602.     WCHAR*                  pwszURL = NULL;
  603.  
  604.     // Prompt for the hostname/ip
  605.     printf("\nPlease enter the IP address of host:\n");
  606.     wscanf(L"%ls", wszHost);
  607.  
  608.     if( FAILED( hr = CreateHostAddress(wszHost ) ) )
  609.     {
  610.         printf("Failed Creating Host Address:  0x%X\n", hr);
  611.         goto LCleanup;
  612.     }
  613.  
  614.     // Now set up the Application Description
  615.     ZeroMemory(&dpAppDesc, sizeof(DPN_APPLICATION_DESC));
  616.     dpAppDesc.dwSize = sizeof(DPN_APPLICATION_DESC);
  617.     dpAppDesc.guidApplication = g_guidApp;
  618.  
  619.     // We now have the host address so lets enum
  620.     if( FAILED( hr = g_pDP->EnumHosts(&dpAppDesc,            // pApplicationDesc
  621.                                         g_pHostAddress,     // pdpaddrHost
  622.                                         g_pDeviceAddress,   // pdpaddrDeviceInfo
  623.                                         NULL, 0,            // pvUserEnumData, size
  624.                                         4,                  // dwEnumCount
  625.                                         0,                  // dwRetryInterval
  626.                                         0,                  // dwTimeOut
  627.                                         NULL,               // pvUserContext
  628.                                         NULL,               // pAsyncHandle
  629.                                         DPNENUMHOSTS_SYNC ) ) )// dwFlags
  630.     {
  631.         printf("Failed Enumerating the Hosts:  0x%X\n", hr);
  632.         goto LCleanup;
  633.     }
  634.  
  635. LCleanup:
  636.     return hr;
  637. }
  638.  
  639.  
  640.  
  641.  
  642. //-----------------------------------------------------------------------------
  643. // Name: CreateDeviceAddress()
  644. // Desc: Creates a device address
  645. //-----------------------------------------------------------------------------
  646. HRESULT CreateDeviceAddress()
  647. {
  648.     HRESULT         hr = S_OK;
  649.  
  650.     // Create our IDirectPlay8Address Device Address
  651.     if( FAILED( hr = CoCreateInstance(CLSID_DirectPlay8Address, NULL,
  652.                                     CLSCTX_INPROC_SERVER,
  653.                                     IID_IDirectPlay8Address,
  654.                                     (LPVOID*) &g_pDeviceAddress ) ) )
  655.     {
  656.         printf("Failed Creating the IDirectPlay8Address Object:  0x%X\n", hr);
  657.         goto LCleanup;
  658.     }
  659.     
  660.     // Set the SP for our Device Address
  661.     if( FAILED( hr = g_pDeviceAddress->SetSP(&CLSID_DP8SP_TCPIP ) ) )
  662.     {
  663.         printf("Failed Setting the Service Provider:  0x%X\n", hr);
  664.         goto LCleanup;
  665.     }
  666.  
  667. LCleanup:
  668.     return hr;
  669. }
  670.  
  671.  
  672.  
  673.  
  674. //-----------------------------------------------------------------------------
  675. // Name: CreateHostAddress()
  676. // Desc: Creates a host address
  677. //-----------------------------------------------------------------------------
  678. HRESULT CreateHostAddress(WCHAR* pwszHost)
  679. {
  680.     HRESULT         hr = S_OK;
  681.  
  682.     // Create our IDirectPlay8Address Host Address
  683.     if( FAILED( hr = CoCreateInstance(CLSID_DirectPlay8Address, NULL,
  684.                                     CLSCTX_INPROC_SERVER,
  685.                                     IID_IDirectPlay8Address,
  686.                                     (LPVOID*) &g_pHostAddress ) ) )
  687.     {
  688.         printf("Failed Creating the IDirectPlay8Address Object:  0x%X\n", hr);
  689.         goto LCleanup;
  690.     }
  691.     
  692.     // Set the SP for our Host Address
  693.     if( FAILED( hr = g_pHostAddress->SetSP(&CLSID_DP8SP_TCPIP ) ) )
  694.     {
  695.         printf("Failed Setting the Service Provider:  0x%X\n", hr);
  696.         goto LCleanup;
  697.     }
  698.  
  699.     // Set the hostname into the address
  700.     if( FAILED( hr = g_pHostAddress->AddComponent(DPNA_KEY_HOSTNAME, pwszHost,
  701.                                                     2*(wcslen(pwszHost) + 1), /*bytes*/
  702.                                                     DPNA_DATATYPE_STRING ) ) )
  703.     {
  704.         printf("Failed Adding Hostname to Host Address:  0x%X\n", hr);
  705.         goto LCleanup;
  706.     }
  707.  
  708. LCleanup:
  709.     return hr;
  710. }
  711.  
  712.  
  713.  
  714.  
  715. //-----------------------------------------------------------------------------
  716. // Name: HostSession()
  717. // Desc: Host a DirectPlay session
  718. //-----------------------------------------------------------------------------
  719. HRESULT HostSession()
  720. {
  721.     HRESULT                 hr = S_OK;
  722.     DPN_APPLICATION_DESC    dpAppDesc;
  723.     WCHAR                   wszSession[128];
  724.  
  725.  
  726.     // Prompt the user for the session name
  727.     printf("\nPlease Enter a Session Name.\n");
  728.     wscanf(L"%ls", wszSession);
  729.  
  730.     // Now set up the Application Description
  731.     ZeroMemory(&dpAppDesc, sizeof(DPN_APPLICATION_DESC));
  732.     dpAppDesc.dwSize = sizeof(DPN_APPLICATION_DESC);
  733.     dpAppDesc.guidApplication = g_guidApp;
  734.     dpAppDesc.pwszSessionName = wszSession;
  735.     dpAppDesc.dwFlags = DPNSESSION_MIGRATE_HOST;
  736.  
  737.     // We are now ready to host the app
  738.     if( FAILED( hr = g_pDP->Host(&dpAppDesc,             // AppDesc
  739.                                 &g_pDeviceAddress, 1,   // Device Address
  740.                                 NULL, NULL,             // Reserved
  741.                                 NULL,                   // Player Context
  742.                                 0 ) ) )                    // dwFlags
  743.     {
  744.         printf("Failed Hosting:  0x%X\n", hr);
  745.         goto LCleanup;
  746.     }
  747.     else
  748.     {
  749.         printf("Currently Hosting...\n");
  750.     }
  751.  
  752.  
  753. LCleanup:
  754.     return hr;
  755. }
  756.  
  757.  
  758.  
  759.  
  760. //-----------------------------------------------------------------------------
  761. // Name: ConnectToSession()
  762. // Desc: Connects to a DirectPlay session
  763. //-----------------------------------------------------------------------------
  764. HRESULT ConnectToSession()
  765. {
  766.     HRESULT                     hr = E_FAIL;
  767.     DPN_APPLICATION_DESC        dpnAppDesc;
  768.     IDirectPlay8Address*        pHostAddress = NULL;
  769.  
  770.  
  771.     ZeroMemory(&dpnAppDesc, sizeof(DPN_APPLICATION_DESC));
  772.     dpnAppDesc.dwSize = sizeof(DPN_APPLICATION_DESC);
  773.     dpnAppDesc.guidApplication = g_guidApp;
  774.  
  775.     // Simply connect to the first one in the list
  776.     EnterCriticalSection(&g_csHostList);
  777.  
  778.     if( g_pHostList && SUCCEEDED(hr = g_pHostList->pHostAddress->Duplicate(&pHostAddress ) ) )
  779.     {
  780.         hr = g_pDP->Connect(&dpnAppDesc,        // pdnAppDesc
  781.                             pHostAddress,       // pHostAddr
  782.                             g_pDeviceAddress,   // pDeviceInfo
  783.                             NULL,               // pdnSecurity
  784.                             NULL,               // pdnCredentials
  785.                             NULL, 0,            // pvUserConnectData/Size
  786.                             NULL,               // pvPlayerContext
  787.                             NULL,               // pvAsyncContext
  788.                             NULL,               // pvAsyncHandle
  789.                             DPNCONNECT_SYNC);   // dwFlags
  790.  
  791.         if( FAILED( hr))
  792.             printf("Failed Connecting to Host:  0x%x\n", hr);
  793.     }
  794.     else
  795.     {
  796.         printf("Failed Duplicating Host Address:  0x%x\n", hr);
  797.     }
  798.  
  799.     LeaveCriticalSection(&g_csHostList);
  800.  
  801.     SAFE_RELEASE(pHostAddress);
  802.     return hr;
  803. }
  804.  
  805.  
  806.  
  807.  
  808. //-----------------------------------------------------------------------------
  809. // Name: SendDirectPlayMessage()
  810. // Desc: Sends a DirectPlay message to all players
  811. //-----------------------------------------------------------------------------
  812. HRESULT SendDirectPlayMessage()
  813. {
  814.     HRESULT         hr = S_OK;
  815.     DPN_BUFFER_DESC dpnBuffer;
  816.     WCHAR           wszData[256];
  817.  
  818.     // Get the data from the user
  819.     printf("\nPlease Enter a String.\n");
  820.     wscanf(L"%ls", wszData);
  821.  
  822.     dpnBuffer.pBufferData = (BYTE*) wszData;
  823.     dpnBuffer.dwBufferSize = 2 * (wcslen(wszData) + 1);
  824.  
  825.     if( FAILED( hr = g_pDP->SendTo(DPNID_ALL_PLAYERS_GROUP,  // dpnid
  826.                                     &dpnBuffer,             // pBufferDesc
  827.                                     1,                      // cBufferDesc
  828.                                     0,                      // dwTimeOut
  829.                                     NULL,                   // pvAsyncContext
  830.                                     NULL,                   // pvAsyncHandle
  831.                                     DPNSEND_SYNC |
  832.                                     DPNSEND_NOLOOPBACK ) ) )   // dwFlags
  833.     {
  834.         printf("Failed Sending Data:  0x%x\n", hr);
  835.     }
  836.     return hr;
  837. }
  838.  
  839.  
  840.  
  841.  
  842. //-----------------------------------------------------------------------------
  843. // Name: Register()
  844. // Desc: Register app as lobby launchable
  845. //-----------------------------------------------------------------------------
  846. HRESULT Register()
  847. {
  848.     HRESULT             hr = S_OK;
  849.     DPL_PROGRAM_DESC    dplDesc;
  850.     WCHAR*              pwszPath = NULL;
  851.     WCHAR*              pwszExecutable = NULL;
  852.     int                 i;
  853.  
  854.     ZeroMemory(&dplDesc, sizeof(DPL_PROGRAM_DESC));
  855.     dplDesc.dwSize = sizeof(DPL_PROGRAM_DESC);
  856.     dplDesc.guidApplication = g_guidApp;
  857.  
  858.     // We need to parse out the path and the exe name from the input value
  859.     for (i = wcslen(g_wszPath); i >=0 && g_wszPath[i] != L'\\'; i--);
  860.  
  861.     pwszPath = new WCHAR[i + 1];
  862.  
  863.     if( pwszPath == NULL)
  864.     {
  865.         printf("Failed parsing path.");
  866.         hr = E_FAIL;
  867.         goto LCleanup;
  868.     }
  869.     
  870.     wcsncpy(pwszPath, g_wszPath, i);
  871.     pwszPath[i] = L'\0';
  872.     pwszExecutable = g_wszPath + i + 1;
  873.  
  874.     dplDesc.pwszApplicationName = pwszExecutable;
  875.     dplDesc.pwszExecutableFilename = pwszExecutable;
  876.     dplDesc.pwszExecutablePath = pwszPath;
  877.  
  878.     hr = g_pLobbyApp->RegisterProgram(&dplDesc, 0);
  879. LCleanup:
  880.     SAFE_DELETE_ARRAY(pwszPath);
  881.  
  882.     return hr;
  883. }
  884.  
  885.  
  886.  
  887.  
  888. //-----------------------------------------------------------------------------
  889. // Name: UnRegister()
  890. // Desc: Unregister app as lobby launchable
  891. //-----------------------------------------------------------------------------
  892. HRESULT UnRegister()
  893. {
  894.     HRESULT     hr = S_OK;
  895.  
  896.     hr = g_pLobbyApp->UnRegisterProgram(&g_guidApp, 0);
  897.  
  898.     return hr;
  899. }
  900.  
  901.  
  902.  
  903.  
  904. //-----------------------------------------------------------------------------
  905. // Name: LobbyLaunch()
  906. // Desc: Host or connect to session based on lobby launch settings
  907. //-----------------------------------------------------------------------------
  908. HRESULT LobbyLaunch()
  909. {
  910.     HRESULT                     hr = S_OK;
  911.     DPL_CONNECTION_SETTINGS*    pSettings = NULL;
  912.     DWORD                       dwSettingsSize = 0;
  913.  
  914.     // Get the lobby connection data
  915.     // First see how big a buffer we need
  916.     hr = g_pLobbyApp->GetConnectionSettings(g_hLobbyHandle, pSettings, &dwSettingsSize, 0);
  917.  
  918.     if( hr != DPNERR_BUFFERTOOSMALL)
  919.     {
  920.         printf("Failed GetConnectionSettings:  0x%x\n", hr);
  921.         goto LCleanup;
  922.     }
  923.  
  924.     pSettings = (DPL_CONNECTION_SETTINGS*) new BYTE[dwSettingsSize];
  925.  
  926.     if( pSettings == NULL)
  927.     {
  928.         printf("Failed Allocating Buffer:  0x%x\n");
  929.         hr = E_FAIL;
  930.         goto LCleanup;
  931.     }
  932.  
  933.     if( FAILED( hr = g_pLobbyApp->GetConnectionSettings(g_hLobbyHandle, pSettings, &dwSettingsSize, 0 ) ) )
  934.     {
  935.         printf("Failed GetConnectionSettings:  0x%x\n", hr);
  936.         goto LCleanup;
  937.     }
  938.  
  939.     if( g_bHost = pSettings->dwFlags & DPLCONNECTSETTINGS_HOST)
  940.     {
  941.         // We are to host the game
  942.         if( FAILED( hr = g_pDP->Host(&pSettings->dpnAppDesc,         // dpnAppDesc
  943.                                     pSettings->ppdp8DeviceAddresses,// prgpDeviceInfo
  944.                                     pSettings->cNumDeviceAddresses, // cDeviceInfo
  945.                                     NULL, NULL,                     // Security
  946.                                     NULL,                           // pvPlayerContext
  947.                                     0 ) ) )                            // dwFlags
  948.         {
  949.             printf("Failed Host:  0x%x\n", hr);
  950.             goto LCleanup;
  951.         }
  952.     }
  953.     else
  954.     {
  955.         // We need to connect
  956.         if( FAILED( hr = g_pDP->Connect(&pSettings->dpnAppDesc,              // pdnAppDesc
  957.                                         pSettings->pdp8HostAddress,         // pHostAddr
  958.                                         pSettings->ppdp8DeviceAddresses[0], // pDeviceInfo
  959.                                         NULL, NULL,                         // Security
  960.                                         NULL, 0,                            // pvUserConnectData/Size
  961.                                         NULL,                               // pvPlayerContext
  962.                                         NULL, NULL,                         // pvAsyncContext/Handle
  963.                                         DPNCONNECT_SYNC ) ) )                  // dwFlags
  964.         {
  965.             printf("Failed Lobby App Connect:  0x%x\n", hr);
  966.             goto LCleanup;
  967.         }
  968.  
  969.     }
  970. LCleanup:
  971.     if( pSettings)
  972.     {
  973.         SAFE_RELEASE(pSettings->pdp8HostAddress);
  974.  
  975.         for (DWORD dwIndex = 0; dwIndex < pSettings->cNumDeviceAddresses; dwIndex++)
  976.         {
  977.             SAFE_RELEASE(pSettings->ppdp8DeviceAddresses[dwIndex]);
  978.         }
  979.     }
  980.     SAFE_DELETE_ARRAY(pSettings);
  981.     return hr;
  982. }
  983.  
  984.  
  985.  
  986.  
  987. //-----------------------------------------------------------------------------
  988. // Name: InitDirectPlayVoice()
  989. // Desc: Initialize DirectPlay Voice
  990. //-----------------------------------------------------------------------------
  991. HRESULT InitDirectPlayVoice()
  992. {
  993.     HRESULT             hr = S_OK;
  994.     DVSESSIONDESC       dvSessionDesc;
  995.     DVSOUNDDEVICECONFIG dvSoundDeviceConfig;
  996.     DVCLIENTCONFIG      dvClientConfig;
  997.     DVID                dvid;
  998.  
  999.     // Init the server if we are the host
  1000.     if( g_bHost )
  1001.     {
  1002.         // Create the IDirectPlayVoiceServer Object
  1003.         if( FAILED( hr = CoCreateInstance(CLSID_DirectPlayVoiceServer, NULL, 
  1004.                                         CLSCTX_INPROC_SERVER,
  1005.                                         IID_IDirectPlayVoiceServer, 
  1006.                                         (LPVOID*) &g_pVoiceServer ) ) )
  1007.         {
  1008.             printf("Failed Creating the IDirectPlayVoiceServer Object:  0x%X\n", hr);
  1009.             goto LCleanup;
  1010.         }
  1011.  
  1012.         // Init the Voice server
  1013.         if( FAILED( hr = g_pVoiceServer->Initialize(g_pDP, DirectVoiceServerMessageHandler, NULL, 0, 0 ) ) )
  1014.         {
  1015.             printf("Failed Initializing the IDirectPlayVoiceServer Object:  0x%X\n", hr);
  1016.             goto LCleanup;
  1017.         }
  1018.  
  1019.         ZeroMemory(&dvSessionDesc, sizeof(DVSESSIONDESC));
  1020.         dvSessionDesc.dwSize = sizeof(DVSESSIONDESC);
  1021.         dvSessionDesc.dwSessionType = DVSESSIONTYPE_PEER;
  1022.         dvSessionDesc.dwBufferQuality = DVBUFFERQUALITY_DEFAULT;
  1023.         dvSessionDesc.guidCT = DPVCTGUID_DEFAULT;
  1024.         dvSessionDesc.dwBufferAggressiveness = DVBUFFERAGGRESSIVENESS_DEFAULT;
  1025.  
  1026.         if( FAILED( hr = g_pVoiceServer->StartSession(&dvSessionDesc, 0 ) ) )
  1027.         {
  1028.             printf("Failed Starting the Session:  0x%X\n", hr);
  1029.             goto LCleanup;
  1030.         }
  1031.     }
  1032.  
  1033.     // Test DirectVoice
  1034.     if( FAILED( hr = TestDirectVoice() ) )
  1035.     {
  1036.         printf("Failed Testing DirectVoice:  0x%X\n", hr);
  1037.         goto LCleanup;
  1038.     }
  1039.  
  1040.     // Init the client and the connection
  1041.     // Create the IDirectPlayVoiceClient Object
  1042.     if( FAILED( hr = CoCreateInstance(CLSID_DirectPlayVoiceClient, NULL, 
  1043.                                     CLSCTX_INPROC_SERVER,
  1044.                                     IID_IDirectPlayVoiceClient, 
  1045.                                     (LPVOID*) &g_pVoiceClient ) ) )
  1046.     {
  1047.         printf("Failed Creating the IDirectPlayVoiceClient Object:  0x%X\n", hr);
  1048.         goto LCleanup;
  1049.     }
  1050.  
  1051.     // Init the Voice client
  1052.     if( FAILED( hr = g_pVoiceClient->Initialize(g_pDP, DirectVoiceClientMessageHandler, NULL, 0, 0 ) ) )
  1053.     {
  1054.         printf("Failed Initializing the IDirectPlayVoiceClient Object:  0x%X\n", hr);
  1055.         goto LCleanup;
  1056.     }
  1057.  
  1058.     ZeroMemory(&dvSoundDeviceConfig, sizeof(DVSOUNDDEVICECONFIG));
  1059.     dvSoundDeviceConfig.dwSize = sizeof(DVSOUNDDEVICECONFIG);
  1060.     dvSoundDeviceConfig.dwFlags = DVSOUNDCONFIG_AUTOSELECT;
  1061.     dvSoundDeviceConfig.guidPlaybackDevice = DSDEVID_DefaultVoicePlayback;
  1062.     dvSoundDeviceConfig.lpdsPlaybackDevice = NULL;
  1063.     dvSoundDeviceConfig.guidCaptureDevice = DSDEVID_DefaultVoiceCapture;
  1064.     dvSoundDeviceConfig.lpdsCaptureDevice = NULL;
  1065.     dvSoundDeviceConfig.hwndAppWindow = GetConsoleHwnd();
  1066.     dvSoundDeviceConfig.lpdsMainBuffer = NULL;
  1067.     dvSoundDeviceConfig.dwMainBufferFlags = 0;
  1068.     dvSoundDeviceConfig.dwMainBufferPriority = 0;
  1069.  
  1070.     ZeroMemory(&dvClientConfig, sizeof(DVCLIENTCONFIG));
  1071.     dvClientConfig.dwSize = sizeof(DVCLIENTCONFIG);
  1072.     dvClientConfig.dwFlags = DVCLIENTCONFIG_AUTOVOICEACTIVATED | DVCLIENTCONFIG_AUTORECORDVOLUME;
  1073.     dvClientConfig.lRecordVolume = DVRECORDVOLUME_LAST;
  1074.     dvClientConfig.lPlaybackVolume = DVPLAYBACKVOLUME_DEFAULT;
  1075.     dvClientConfig.dwThreshold = DVTHRESHOLD_UNUSED;
  1076.     dvClientConfig.dwBufferQuality = DVBUFFERQUALITY_DEFAULT;
  1077.     dvClientConfig.dwBufferAggressiveness = DVBUFFERAGGRESSIVENESS_DEFAULT;
  1078.     dvClientConfig.dwNotifyPeriod = 0;
  1079.  
  1080.     if( FAILED( hr = g_pVoiceClient->Connect(&dvSoundDeviceConfig, &dvClientConfig, DVFLAGS_SYNC ) ) )
  1081.     {
  1082.         printf("Failed DirectVoice Client Connect:  0x%X\n", hr);
  1083.         goto LCleanup;
  1084.     }
  1085.  
  1086.     dvid = DVID_ALLPLAYERS;
  1087.     if( FAILED( hr = g_pVoiceClient->SetTransmitTargets(&dvid, 1, 0 ) ) )
  1088.     {
  1089.         printf("Failed SetTransmitTargets:  0x%X\n", hr);
  1090.         goto LCleanup;
  1091.     }
  1092.  
  1093. LCleanup:
  1094.     return hr;
  1095. }
  1096.  
  1097.  
  1098.  
  1099.  
  1100. //-----------------------------------------------------------------------------
  1101. // Name: TestDirectVoice()
  1102. // Desc: Tests that DirectPlay Voice is has been setup, and if it hasn't,
  1103. //       it runs the DirectPlay Voice setup wizard.
  1104. //-----------------------------------------------------------------------------
  1105. HRESULT TestDirectVoice()
  1106. {
  1107.     HRESULT                 hr = S_OK;
  1108.     IDirectPlayVoiceTest*   pVoiceTest = NULL;
  1109.     GUID                    guidPlayback;
  1110.     GUID                    guidCapture;
  1111.     HWND                    hwnd;
  1112.  
  1113.     // Create the IDirectPlayVoiceTest Object
  1114.     if( FAILED( hr = CoCreateInstance(CLSID_DirectPlayVoiceTest, NULL, 
  1115.                                     CLSCTX_INPROC_SERVER,
  1116.                                     IID_IDirectPlayVoiceTest, 
  1117.                                     (LPVOID*) &pVoiceTest ) ) )
  1118.     {
  1119.         printf("Failed Creating the IDirectPlayVoiceTest Object:  0x%X\n", hr);
  1120.         goto LCleanup;
  1121.     }
  1122.  
  1123.     guidPlayback = DSDEVID_DefaultVoicePlayback;
  1124.     guidCapture = DSDEVID_DefaultVoiceCapture;
  1125.  
  1126.     hr = pVoiceTest->CheckAudioSetup(&guidPlayback, &guidCapture, NULL, DVFLAGS_QUERYONLY);
  1127.  
  1128.     if( hr == DVERR_RUNSETUP)
  1129.     {
  1130.         hwnd = GetConsoleHwnd();
  1131.  
  1132.         if( FAILED( hr = pVoiceTest->CheckAudioSetup(&guidPlayback, &guidCapture, hwnd, DVFLAGS_ALLOWBACK ) ) )
  1133.         {
  1134.             printf("Failed CheckAudioSetup:  0x%X\n", hr);
  1135.             goto LCleanup;
  1136.         }
  1137.     }
  1138.     else if( FAILED( hr))
  1139.     {
  1140.         printf("Failed CheckAudioSetup:  0x%X\n", hr);
  1141.         goto LCleanup;
  1142.     }
  1143.  
  1144. LCleanup:
  1145.     SAFE_RELEASE(pVoiceTest);
  1146.     return hr;
  1147. }
  1148.  
  1149.  
  1150.  
  1151.  
  1152. //-----------------------------------------------------------------------------
  1153. // Name: GetConsoleHwnd()
  1154. // Desc: Returns the console HWND
  1155. //-----------------------------------------------------------------------------
  1156. HWND GetConsoleHwnd()
  1157. {
  1158.     HWND hwndFound;        // This is what is returned to the caller.
  1159.     char pszNewWindowTitle[1024]; // Contains fabricated WindowTitle.
  1160.     char pszOldWindowTitle[1024]; // Contains original WindowTitle.
  1161.  
  1162.     // Fetch current window title.
  1163.     GetConsoleTitle(pszOldWindowTitle, 1024);
  1164.  
  1165.     // Format a "unique" NewWindowTitle.
  1166.     wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
  1167.  
  1168.     // Change current window title.
  1169.     SetConsoleTitle(pszNewWindowTitle);
  1170.  
  1171.     // Ensure window title has been updated.
  1172.     Sleep(40);
  1173.  
  1174.     // Look for NewWindowTitle.
  1175.     hwndFound=FindWindow(NULL, pszNewWindowTitle);
  1176.  
  1177.     // Restore original window title.
  1178.     SetConsoleTitle(pszOldWindowTitle);
  1179.  
  1180.     return(hwndFound);
  1181. }
  1182.  
  1183.  
  1184.  
  1185.  
  1186. //-----------------------------------------------------------------------------
  1187. // Name: CleanupDirectPlay()
  1188. // Desc: Cleanup DirectPlay
  1189. //-----------------------------------------------------------------------------
  1190. void CleanupDirectPlay()
  1191. {
  1192.     HOST_NODE*                  pHostNode = NULL;
  1193.     HOST_NODE*                  pHostNodetmp = NULL;
  1194.  
  1195.     // Shutdown DirectVoice
  1196.     if( g_pVoiceClient)
  1197.     {
  1198.         g_pVoiceClient->Disconnect(DVFLAGS_SYNC);
  1199.     }
  1200.     if( g_pVoiceServer)
  1201.     {
  1202.         g_pVoiceServer->StopSession(0);
  1203.     }
  1204.  
  1205.     // Shutdown DirectPlay
  1206.     if( g_pDP)
  1207.         g_pDP->Close(0);
  1208.  
  1209.     if( g_pLobbyApp)
  1210.         g_pLobbyApp->Close(0);
  1211.  
  1212.     // Clean up Host list
  1213.     EnterCriticalSection(&g_csHostList);
  1214.     
  1215.     pHostNode = g_pHostList;
  1216.     while( pHostNode != NULL )
  1217.     {       
  1218.         SAFE_RELEASE(pHostNode->pHostAddress);
  1219.         SAFE_DELETE(pHostNode->pAppDesc);
  1220.         SAFE_DELETE(pHostNode->pwszSessionName);
  1221.  
  1222.         pHostNodetmp = pHostNode;
  1223.         pHostNode    = pHostNode->pNext;
  1224.         SAFE_DELETE(pHostNodetmp);
  1225.     }
  1226.  
  1227.     LeaveCriticalSection(&g_csHostList);
  1228.  
  1229.     SAFE_RELEASE(g_pDeviceAddress);
  1230.     SAFE_RELEASE(g_pHostAddress);
  1231.     SAFE_RELEASE(g_pDP);
  1232.     SAFE_RELEASE(g_pLobbyApp);
  1233.     SAFE_RELEASE(g_pVoiceServer);
  1234.     SAFE_RELEASE(g_pVoiceClient);
  1235.     SAFE_DELETE_ARRAY(g_wszPath);
  1236.  
  1237.     DeleteCriticalSection(&g_csHostList);
  1238. }
  1239.     
  1240.